package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.commonutils.results;
import com.entity.pojo.CategoriesRelation;
import com.entity.pojo.Courses;
import com.entity.pojo.CoursesRelation;
import com.service.CoursesRelationService;
import com.service.CoursesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenMo
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/coursesRelation")
@CrossOrigin
public class CoursesRelationController {
    @Resource
    private CoursesRelationService coursesRelationService;

    @Resource
    private CoursesService coursesService;

    @ApiOperation("根据课程id查询后继课程列表")
    @GetMapping("/getNextCourseList/{courseId}")
    public results getNextCourseList(@PathVariable Integer courseId) {
        List<CoursesRelation> coursesRelationList = coursesRelationService
                .list(new QueryWrapper<CoursesRelation>().eq("courseId", courseId));
        List<Integer> NextCourseIds = coursesRelationList.stream()
                .map(CoursesRelation::getNextCourseId)
                .collect(Collectors.toList());
        // 用来动态渲染关联课程标签的颜色
        List<Integer> relationLevelList = coursesRelationList.stream()
                .map(CoursesRelation::getRelationLevel)
                .collect(Collectors.toList());
        if (!NextCourseIds.isEmpty()) {
            Collection<Courses> nextCourseList = coursesService.listByIds(NextCourseIds);
            return results.ok().data("nextCourseList", nextCourseList).data("relationLevelList",relationLevelList);
        }
        return results.ok().data("nextCourseList", null).data("relationLevelList",null);
    }

    @ApiOperation("根据课程id查询可以添加的后继课程列表")
    @GetMapping("/getCanBeAddedNextCourseList/{currentCourseId}")
    public results getCanBeAddedNextCourseList(@PathVariable Integer currentCourseId) {
        List<Courses> canBeAddedNextCourseList = coursesRelationService.getCanBeAddedNextCourseList(currentCourseId);
        return results.ok().data("canBeAddedNextCourseList", canBeAddedNextCourseList);
    }

    @ApiOperation("增加新的关联课程")
    @PostMapping("addCourseRelation")
    public results addCourseRelation(@RequestBody CoursesRelation coursesRelation) {
        if (coursesRelation.getNextCourseId() == null) {
            return results.error("关联课程不能为空");
        }
        if (coursesRelation.getRelationLevel() == null) {
            return results.error("关联等级不能为空");
        }
        boolean saveFlag = coursesRelationService.addCourseRelation(coursesRelation);
        if (saveFlag) {
            return results.ok();
        } else {
            return results.error("增加后继课程信息失败");
        }
    }

    @ApiOperation("删除后继课程关系")
    @DeleteMapping("deleteCourseRelation/{courseId}/{nextCourseId}")
    public results deleteCourseRelation(@PathVariable Integer courseId, @PathVariable Integer nextCourseId) {
        boolean deleteFlag = coursesRelationService.deleteCourseRelation(courseId, nextCourseId);
        if (deleteFlag) {
            return results.ok();
        } else {
            return results.error("删除后继课程失败");
        }
    }
}

